package com.kanshu.ksgb.fastread.doudou.module.book.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.widget.CheckBox;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.adapter.BaseViewHolder;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.common.business.manager.MMKVUserManager;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.module.book.bean.RecentBookInfo;
import com.kanshu.ksgb.fastread.doudou.common.business.manager.CacheManager;
import com.kanshu.ksgb.fastread.doudou.module.reader.utils.SettingManager;

import java.util.List;

/**
 * 最近阅读适配器
 * Created by wcy on 2017\6\21 0021.
 */

public class BookRecentAdapter extends BaseQuickAdapter<RecentBookInfo> {

    private boolean isEditable;

    public BookRecentAdapter(Context context, List<RecentBookInfo> data) {
        super(context, data);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.item_recent_layout;
    }

    @Override
    protected void convert(BaseViewHolder holder, RecentBookInfo item, int position) {
        GlideImageLoader.load(item.cover_url, holder.getView(R.id.cover));
        //阅读进度 及 书籍信息
        String order = item.c_order;
        if (SettingManager.getInstance().isReadBook(item.book_id)) {
            order = String.valueOf(SettingManager.getInstance().getCurReadProgress(item.book_id).chapter);
        }
        int orders = 0;
        if (!TextUtils.isEmpty(order)) {
            orders = Integer.parseInt(order);
        }
        DisplayUtils.gone(holder.getView(R.id.book_title));
        if (orders == 0) {
            holder.setText(R.id.book_progress, "未阅读");
        } else {
            holder.setText(R.id.book_progress, "已阅读至" + order + "章");
        }

        holder.setText(R.id.book_title, item.book_title);
        holder.getView(R.id.root).setTag(position);
        // 编辑模式
        final CheckBox checkBox = holder.getView(R.id.checkbox);
        checkBox.setChecked(false);
        checkBox.setFocusable(false);
        DisplayUtils.gone(holder.getView(R.id.download_chapter_count));
        if (isEditable) {
            DisplayUtils.visible(checkBox);
            checkBox.setChecked(item.is_selected);
        } else {
            DisplayUtils.invisible(checkBox);
            int download_count = MMKVUserManager.getInstance().getBookDownloadChapterCount(item.book_id);
            if(download_count != 0){
                DisplayUtils.visible(holder.getView(R.id.download_chapter_count));
                holder.setText(R.id.download_chapter_count,  "已下载" + (TextUtils.equals(String.valueOf(download_count), item.chapter_count) ? "" : download_count + "章"));
            }
        }
    }

    public void setEditable(boolean editable) {
        isEditable = editable;
        notifyDataSetChanged();
    }

    public boolean isEditable() {
        return isEditable;
    }
}
